﻿using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using StudySystem.MainLogic.Others;
using StudySystem.SQLServer;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

namespace StudySystem.MainLogic.Items
{
    public class ClassSubject
    {
        //UserInformation _user;//记录用户ID
        string _subjectID;//记录课程ID
        string _subjectName;//课程名称
        //int _quesCount;//课程拥有的问题数量
        List<ClassQuestion> _questions;//课程拥有的所有问题
        public ClassSubject()
        {

        }
        public ClassSubject(DataRow dr)
        {
            //User = dr["UserID"].ToString();
            SubjectID = dr["SubjectID"].ToString();
            SubjectName = dr["SubjectName"].ToString();
            //QuesCount = Convert.ToInt32(dr["QuesCount"]);
            GetAllQuestion();
        }

        //public string User { get => _user.UserID; set => _user.UserID = value; }
        public string SubjectID { get => _subjectID; set => _subjectID = value; }
        public string SubjectName { get => _subjectName; set => _subjectName = value; }
        public int QuesCount { get => Questions.Count; }
        public List<ClassQuestion> Questions { get => _questions; set => _questions = value; }

        public static bool CheckSubName(string subName, List<ClassSubject> subjects)
        {
            bool ret = true;
            foreach (ClassSubject s in subjects)
            {
                if (subName == s.SubjectName)
                {
                    return ret;
                }
            }
            return ret;
        }

        public static List<ClassSubject> InsertASubject(string subjectName)
        {
            //在DB上插入数据
            DBForSubject.InsertASubject(subjectName);
            //dt更新
            DBForSubject.GetSubjects(UserManagement.user.UserID, ref UserManagement.ds);
            //根据dt生成list
            return UserManagement.user.GetSubjectsOfUser();
        }

        public int DeleteASubject(List<ClassSubject> subjects)
        {
            //删除ds中的学科   更新ds
            DataTable dt = UserManagement.ds.Tables["SubjectTable"];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                if (dr["SubjectID"].ToString() == SubjectID)
                {
                    dt.Rows.Remove(dr);
                    break;
                }
            }
            //删除list的数据   更新list
            for (int i = 0; i < subjects.Count; i++)
            {
                ClassSubject s = subjects[i];
                if (s.SubjectID == SubjectID)
                {
                    subjects.Remove(s);
                    break;
                }
            }
            //删除数据库中的学科  更新数据库
            return DBForSubject.DeleteASubject(SubjectID);
        }


        public void ModifySubName(string newName)
        {
            //更新数据库
            DBForSubject.ModifySubName(SubjectID, newName);
            //更新ds
            foreach (DataRow dataRow in UserManagement.ds.Tables["SubjectTable"].Rows)
            {
                if (dataRow["SubjectID"].ToString() == SubjectID)
                {
                    dataRow["SubjectName"] = newName;
                    break;
                }
            }
            //更新list
            this.SubjectName = newName;
        }

        /// <summary>
        /// 获得当前对象学科所拥有的全部问题
        /// </summary>
        public List<ClassQuestion> GetAllQuestion()
        {
            foreach (List<ClassQuestion> quesList in UserManagement.listQuestions)
            {
                if (quesList[0].SubjectID == this.SubjectID)
                {
                    this.Questions = quesList;
                    return quesList;
                }
            }
            return null;
        }

        /// <summary>
        /// 随机获得当前学科指定数量的问题
        /// </summary>
        /// <param name="questionCount"></param>
        /// <returns></returns>
        public List<ClassQuestion> GetQuestions(int questionCount)
        {
            return Utilities.Random<ClassQuestion>(this.Questions, questionCount, false);
        }

        


    }
}
